﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnitySheet;

public class SetCellTextSize : MonoBehaviour {

    public Slider fontSizeSlider;
    public Text fontSizeText;
    [Range(1, 55)]
    public int MaxFontSize = 35;
    // Use this for initialization
    void Start () {
        fontSizeSlider.wholeNumbers = true;
        fontSizeSlider.minValue = 1;
        fontSizeSlider.maxValue = MaxFontSize;
        fontSizeText.text = ((int)fontSizeSlider.value).ToString();
        fontSizeSlider.onValueChanged.AddListener(OnValueChanged);
        SheetManager.sheetManager.sheetOperator.onSelectCell += OnSelectedCellsChanged;
        SheetManager.sheetManager.sheetOperator.onDeselectCell += OnSelectedCellsChanged;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    void OnValueChanged(float size)
    {
        fontSizeText.text = ((int)size).ToString();
        Cell[] cells = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        foreach (var item in cells)
        {
            ((ContentCell)item).description.fontSize = (int)size;
        }
    }

    void OnSelectedCellsChanged(Cell cell)
    {
        Cell[] cells = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        if (cells.Length > 0)
        {
            int s = ((ContentCell)cells[0]).description.fontSize;
            foreach (var item in cells)
            {
                if (s != ((ContentCell)item).description.fontSize)
                {
                    s = MaxFontSize / 2;
                    fontSizeSlider.onValueChanged.RemoveListener(OnValueChanged);
                    fontSizeSlider.value = s;
                    fontSizeText.text = "-";
                    fontSizeSlider.onValueChanged.AddListener(OnValueChanged);
                    return;
                }
            }
            fontSizeSlider.value = s;
        }
    }
}
